# 20_jes2001_2016_participation_in_election_interaction_term.R
# Purpose: Ideological Extremism and Political Participation in Japan
# Created: 2021-7-19 Taka-aki Asano
# Last Modified: 2022-1-4

# package
require("haven")
require("dplyr")
require("tidyr")
require("ggplot2")
require("texreg")
quartzFonts(HiraKaku = quartzFont(rep('Hiragino Kaku Gothic Pro W3', 4)))
theme_set(theme_classic(base_size = 12, base_family = 'HiraKaku'))


# 2001
## merge
JES2001_Ideology <- JES2001_Respondent[,c("ID", "Ideology")]
JES2001_Ideology <- as.data.frame(JES2001_Ideology)
JES2001_Ideology <- left_join(JES2001_Ideology, 
                              JES2001[,c("ID", "GENDER", "EDUCAT01", "BORNY", "B1", "A7")], 
                              by = "ID")

## recode
JES2001_Ideology$Vote <- NA
JES2001_Ideology$Vote[JES2001_Ideology$B1 %in% c(1, 2)] <- 1
JES2001_Ideology$Vote[JES2001_Ideology$B1 == 3] <- 0
JES2001_Ideology$Ideology_strong <- abs(JES2001_Ideology$Ideology - 
                                          median(JES2001_Ideology$Ideology, na.rm = TRUE))
JES2001_Ideology$A7[JES2001_Ideology$A7 %in% c(88, 99)] <- NA
JES2001_Ideology$Support <- ifelse(JES2001_Ideology$A7 == 9, 0, 1)
JES2001_Ideology$Female <- ifelse(JES2001_Ideology$GENDER == 2, 1, 0)
JES2001_Ideology$EDUCAT01[JES2001_Ideology$EDUCAT01 == 9] <- NA
JES2001_Ideology$College <- ifelse(JES2001_Ideology$EDUCAT01 == 4, 1, 0)
JES2001_Ideology$Age <- 2001 - JES2001_Ideology$BORNY

## regression
glm2001 <- glm(Vote ~ Ideology*Ideology_strong + Support + Female + College + Age, 
               data = JES2001_Ideology, family = binomial(link = "logit"))
summary(glm2001)


# 2003
## merge
JES2003_Ideology <- JES2003_Respondent[,c("ID", "Ideology")]
JES2003_Ideology <- as.data.frame(JES2003_Ideology)
JES2003_Ideology <- left_join(JES2003_Ideology, 
                              JES2003[,c("ID", "GENDER", "EDUCAT03", "BORNY", "E1", "D8")], 
                              by = "ID")

## recode
JES2003_Ideology$Vote <- NA
JES2003_Ideology$Vote[JES2003_Ideology$E1 %in% c(1, 2)] <- 1
JES2003_Ideology$Vote[JES2003_Ideology$E1 == 3] <- 0
JES2003_Ideology$Ideology_strong <- abs(JES2003_Ideology$Ideology - 
                                          median(JES2003_Ideology$Ideology, na.rm = TRUE))
JES2003_Ideology$D8[JES2003_Ideology$D8 %in% c(88, 99)] <- NA
JES2003_Ideology$Support <- ifelse(JES2003_Ideology$D8 == 9, 0, 1)
JES2003_Ideology$Female <- ifelse(JES2003_Ideology$GENDER == 2, 1, 0)
JES2003_Ideology$EDUCAT03[JES2003_Ideology$EDUCAT03 == 9] <- NA
JES2003_Ideology$College <- ifelse(JES2003_Ideology$EDUCAT03 == 4, 1, 0)
JES2003_Ideology$Age <- 2003 - JES2003_Ideology$BORNY

## regression
glm2003 <- glm(Vote ~ Ideology*Ideology_strong + Support + Female + College + Age, 
               data = JES2003_Ideology, family = binomial(link = "logit"))
summary(glm2003)


# 2004
## merge
JES2004_Ideology <- JES2004_Respondent[,c("ID", "Ideology")]
JES2004_Ideology <- as.data.frame(JES2004_Ideology)
JES2004_Ideology <- left_join(JES2004_Ideology, 
                              JES2004[,c("ID", "GENDER", "EDUCAT04", "BORNY", "H1", "G8")], 
                              by = "ID")

## recode
JES2004_Ideology$Vote <- NA
JES2004_Ideology$Vote[JES2004_Ideology$H1 %in% c(1, 2)] <- 1
JES2004_Ideology$Vote[JES2004_Ideology$H1 == 3] <- 0
JES2004_Ideology$Ideology_strong <- abs(JES2004_Ideology$Ideology - 
                                          median(JES2004_Ideology$Ideology, na.rm = TRUE))
JES2004_Ideology$G8[JES2004_Ideology$G8 %in% c(88, 99)] <- NA
JES2004_Ideology$Support <- ifelse(JES2004_Ideology$G8 == 9, 0, 1)
JES2004_Ideology$Female <- ifelse(JES2004_Ideology$GENDER == 2, 1, 0)
JES2004_Ideology$EDUCAT04[JES2004_Ideology$EDUCAT04 == 9] <- NA
JES2004_Ideology$College <- ifelse(JES2004_Ideology$EDUCAT04 == 4, 1, 0)
JES2004_Ideology$Age <- 2004 - JES2004_Ideology$BORNY

## regression
glm2004 <- glm(Vote ~ Ideology*Ideology_strong + Support + Female + College + Age, 
               data = JES2004_Ideology, family = binomial(link = "logit"))
summary(glm2004)


# 2005
## merge
JES2005_Ideology <- JES2005_Respondent[,c("ID", "Ideology")]
JES2005_Ideology <- as.data.frame(JES2005_Ideology)
JES2005_Ideology <- left_join(JES2005_Ideology, 
                              JES2005[,c("ID", "GENDER", "EDUCAT05", "BORNY", "K1", "J10")], 
                              by = "ID")

## recode
JES2005_Ideology$Vote <- NA
JES2005_Ideology$Vote[JES2005_Ideology$K1 %in% c(1, 2)] <- 1
JES2005_Ideology$Vote[JES2005_Ideology$K1 == 3] <- 0
JES2005_Ideology$Ideology_strong <- abs(JES2005_Ideology$Ideology - 
                                          median(JES2005_Ideology$Ideology, na.rm = TRUE))
JES2005_Ideology$J10[JES2005_Ideology$J10 %in% c(88, 99)] <- NA
JES2005_Ideology$Support <- ifelse(JES2005_Ideology$J10 == 9, 0, 1)
JES2005_Ideology$Female <- ifelse(JES2005_Ideology$GENDER == 2, 1, 0)
JES2005_Ideology$EDUCAT05[JES2005_Ideology$EDUCAT05 == 9] <- NA
JES2005_Ideology$College <- ifelse(JES2005_Ideology$EDUCAT05 == 4, 1, 0)
JES2005_Ideology$Age <- 2005 - JES2005_Ideology$BORNY

## regression
glm2005 <- glm(Vote ~ Ideology*Ideology_strong + Support + Female + College + Age, 
               data = JES2005_Ideology, family = binomial(link = "logit"))
summary(glm2005)


# 2007
## merge
JES2007_Ideology <- JES2007_Respondent[,c("ID", "Ideology")]
JES2007_Ideology <- as.data.frame(JES2007_Ideology)
JES2007_Ideology <- left_join(JES2007_Ideology, 
                              JES2007[,c("ID", "F1", "F4", "F2_AGE", "Q1", "Q20")], 
                              by = "ID")

## recode
JES2007_Ideology$Vote <- NA
JES2007_Ideology$Vote[JES2007_Ideology$Q1 %in% c(1, 2)] <- 1
JES2007_Ideology$Vote[JES2007_Ideology$Q1 == 3] <- 0
JES2007_Ideology$Ideology_strong <- abs(JES2007_Ideology$Ideology - 
                                          median(JES2007_Ideology$Ideology, na.rm = TRUE))
JES2007_Ideology$Q20[JES2007_Ideology$Q20 %in% c(88, 99)] <- NA
JES2007_Ideology$Support <- ifelse(JES2007_Ideology$Q20 == 9, 0, 1)
JES2007_Ideology$Female <- ifelse(JES2007_Ideology$F1 == 2, 1, 0)
JES2007_Ideology$F4[JES2007_Ideology$F4 == 9] <- NA
JES2007_Ideology$College <- ifelse(JES2007_Ideology$F4 == 4, 1, 0)

## regression
glm2007 <- glm(Vote ~ Ideology*Ideology_strong + Support + Female + College + F2_AGE, 
               data = JES2007_Ideology, family = binomial(link = "logit"))
summary(glm2007)


# 2009
## merge
JES2009_Ideology <- JES2009_Respondent[,c("ID", "Ideology")]
JES2009_Ideology <- as.data.frame(JES2009_Ideology)
JES2009_Ideology <- left_join(JES2009_Ideology, 
                              JES2009[,c("ID", "F1", "F4", "F2_AGE", "Q10")], 
                              by = "ID")
JES2009_2 <- read_sav(
  "~/Dropbox/Dataset/JESIV/0999_wave3.sav", 
  user_na = FALSE
)
JES2009_Ideology <- left_join(JES2009_Ideology, 
                              JES2009_2[,c("ID", "Q1")], 
                              by = "ID")

## recode
JES2009_Ideology$Vote <- NA
JES2009_Ideology$Vote[JES2009_Ideology$Q1 %in% c(1, 2)] <- 1
JES2009_Ideology$Vote[JES2009_Ideology$Q1 == 3] <- 0
JES2009_Ideology$Ideology_strong <- abs(JES2009_Ideology$Ideology - 
                                          median(JES2009_Ideology$Ideology, na.rm = TRUE))
JES2009_Ideology$Q10[JES2009_Ideology$Q10 %in% c(88, 99)] <- NA
JES2009_Ideology$Support <- ifelse(JES2009_Ideology$Q10 == 9, 0, 1)
JES2009_Ideology$Female <- ifelse(JES2009_Ideology$F1 == 2, 1, 0)
JES2009_Ideology$F4[JES2009_Ideology$F4 == 9] <- NA
JES2009_Ideology$College <- ifelse(JES2009_Ideology$F4 == 4, 1, 0)

## regression
glm2009 <- glm(Vote ~ Ideology*Ideology_strong + Support + Female + College + F2_AGE, 
               data = JES2009_Ideology, family = binomial(link = "logit"))
summary(glm2009)


# 2010
## merge
JES2010_Ideology <- JES2010_Respondent[,c("ID", "Ideology")]
JES2010_Ideology <- as.data.frame(JES2010_Ideology)
JES2010_Ideology <- left_join(JES2010_Ideology, 
                              JES2010[,c("ID", "F1", "F4", "F2_AGE", "Q8")], 
                              by = "ID")
JES2010_2 <- read_sav(
  "~/Dropbox/Dataset/JESIV/0999_wave6.sav", 
  user_na = FALSE
)
JES2010_Ideology <- left_join(JES2010_Ideology, 
                              JES2010_2[,c("ID", "Q1")], 
                              by = "ID")

## recode
JES2010_Ideology$Vote <- NA
JES2010_Ideology$Vote[JES2010_Ideology$Q1 %in% c(1, 2)] <- 1
JES2010_Ideology$Vote[JES2010_Ideology$Q1 == 3] <- 0
JES2010_Ideology$Ideology_strong <- abs(JES2010_Ideology$Ideology - 
                                          median(JES2010_Ideology$Ideology, na.rm = TRUE))
JES2010_Ideology$Q8[JES2010_Ideology$Q8 %in% c(88, 99)] <- NA
JES2010_Ideology$Support <- ifelse(JES2010_Ideology$Q8 == 11, 0, 1)
JES2010_Ideology$Female <- ifelse(JES2010_Ideology$F1 == 2, 1, 0)
JES2010_Ideology$F4[JES2010_Ideology$F4 == 9] <- NA
JES2010_Ideology$College <- ifelse(JES2010_Ideology$F4 == 4, 1, 0)

## regression
glm2010 <- glm(Vote ~ Ideology*Ideology_strong + Support + Female + College + F2_AGE, 
               data = JES2010_Ideology, family = binomial(link = "logit"))
summary(glm2010)


# 2012
## merge
JES2012_Ideology <- JES2012_Respondent[,c("id", "Ideology")]
JES2012_Ideology <- as.data.frame(JES2012_Ideology)
JES2012_Ideology <- left_join(JES2012_Ideology, 
                              JES2012[,c("id", "f1", "f4", "f2", "q4", "q54")], 
                              by = "id")

## recode
JES2012_Ideology$Vote <- NA
JES2012_Ideology$Vote[JES2012_Ideology$q4 == 1] <- 1
JES2012_Ideology$Vote[JES2012_Ideology$q4 == 2] <- 0
JES2012_Ideology$Ideology_strong <- abs(JES2012_Ideology$Ideology - 
                                          median(JES2012_Ideology$Ideology, na.rm = TRUE))
JES2012_Ideology$Support <- ifelse(JES2012_Ideology$q54 == 15, 0, 1)
JES2012_Ideology$Female <- ifelse(JES2012_Ideology$f1 == 2, 1, 0)
JES2012_Ideology$f4[JES2012_Ideology$f4 == 9] <- NA
JES2012_Ideology$College <- ifelse(JES2012_Ideology$f4 == 4, 1, 0)

## regression
glm2012 <- glm(Vote ~ Ideology*Ideology_strong + Support + Female + College + f2, 
               data = JES2012_Ideology, family = binomial(link = "logit"))
summary(glm2012)


# 2013
## merge
JES2013_Ideology <- JES2013_Respondent[,c("id", "Ideology")]
JES2013_Ideology <- left_join(JES2013_Ideology, 
                              JES2013[,c("id", "af1", "af4", "af2_age", "b1x1", "a12x1")], 
                              by = "id")

## recode
JES2013_Ideology$Vote <- NA
JES2013_Ideology$Vote[JES2013_Ideology$b1x1 %in% c(1, 2)] <- 1
JES2013_Ideology$Vote[JES2013_Ideology$b1x1 == 3] <- 0
JES2013_Ideology$Ideology_strong <- abs(JES2013_Ideology$Ideology - 
                                          median(JES2013_Ideology$Ideology, na.rm = TRUE))
JES2013_Ideology$Support <- ifelse(JES2013_Ideology$a12x1 == 8, 0, 1)
JES2013_Ideology$Female <- ifelse(JES2013_Ideology$af1 == 2, 1, 0)
JES2013_Ideology$af4[JES2013_Ideology$af4 == 9] <- NA
JES2013_Ideology$College <- ifelse(JES2013_Ideology$af4 == 4, 1, 0)

## regression
glm2013 <- glm(Vote ~ Ideology*Ideology_strong + Support + Female + College + af2_age, 
               data = JES2013_Ideology, family = binomial(link = "logit"))
summary(glm2013)


# 2014
## merge
JES2014_Ideology <- JES2014_Respondent[,c("id", "Ideology")]
JES2014_Ideology <- left_join(JES2014_Ideology, 
                              JES2014[,c("id", "sex", "a55", "age", "b1", "a7")], 
                              by = "id")

## recode
JES2014_Ideology$Vote <- NA
JES2014_Ideology$Vote[JES2014_Ideology$b1 %in% c(1, 2)] <- 1
JES2014_Ideology$Vote[JES2014_Ideology$b1 == 3] <- 0
JES2014_Ideology$Ideology_strong <- abs(JES2014_Ideology$Ideology - 
                                          median(JES2014_Ideology$Ideology, na.rm = TRUE))
JES2014_Ideology$Support <- ifelse(JES2014_Ideology$a7 == 9, 0, 1)
JES2014_Ideology$Female <- ifelse(JES2014_Ideology$sex == 2, 1, 0)
JES2014_Ideology$a55[JES2014_Ideology$a55 == 9] <- NA
JES2014_Ideology$College <- ifelse(JES2014_Ideology$a55 == 4, 1, 0)

## regression
glm2014 <- glm(Vote ~ Ideology*Ideology_strong + Support + Female + College + age, 
               data = JES2014_Ideology, family = binomial(link = "logit"))
summary(glm2014)


# 2016
## merge
JES2016_Ideology <- JES2016_Respondent[,c("id", "Ideology")]
JES2016_Ideology <- left_join(JES2016_Ideology, 
                              JES2016[,c("id", "b_F1_SEX", "F4", "b_F2_AGE", "b_Q1", "Q5")], 
                              by = "id")

## recode
JES2016_Ideology$Vote <- NA
JES2016_Ideology$Vote[JES2016_Ideology$b_Q1 %in% c(1, 2)] <- 1
JES2016_Ideology$Vote[JES2016_Ideology$b_Q1 == 3] <- 0
JES2016_Ideology$Ideology_strong <- abs(JES2016_Ideology$Ideology - 
                                          median(JES2016_Ideology$Ideology, na.rm = TRUE))
JES2016_Ideology$Support <- ifelse(JES2016_Ideology$Q5 == 7, 0, 1)
JES2016_Ideology$Female <- ifelse(JES2016_Ideology$b_F1_SEX == 2, 1, 0)
JES2016_Ideology$F4[JES2016_Ideology$F4 == 9] <- NA
JES2016_Ideology$College <- ifelse(JES2016_Ideology$F4 == 4, 1, 0)

## regression
glm2016 <- glm(Vote ~ Ideology*Ideology_strong + Support + Female + College + b_F2_AGE, 
               data = JES2016_Ideology, family = binomial(link = "logit"))
summary(glm2016)


# plot
glmAll <- data.frame(
  Year = rep(c(2001, 2003, 2004, 2005, 2007, 2009, 2010, 2012, 2013, 2014, 2016), each = 8), 
  Variable = rep(c("Intercept", "Ideology", "Ideological Extremism", "Partisanship", "Female", 
                   "College Education", "Age", "Ideology × Ideological Extremism"), times = 11), 
  Estimate = c(coef(glm2001), coef(glm2003), coef(glm2004), coef(glm2005), 
               coef(glm2007), coef(glm2009), coef(glm2010), coef(glm2012), 
               coef(glm2013), coef(glm2014), coef(glm2016)), 
  SE = c(summary(glm2001)$coefficient[,2], summary(glm2003)$coefficient[,2], 
         summary(glm2004)$coefficient[,2], summary(glm2005)$coefficient[,2], 
         summary(glm2007)$coefficient[,2], summary(glm2009)$coefficient[,2], 
         summary(glm2010)$coefficient[,2], summary(glm2012)$coefficient[,2], 
         summary(glm2013)$coefficient[,2], summary(glm2014)$coefficient[,2], 
         summary(glm2016)$coefficient[,2])
)
glmAll$Variable <- factor(glmAll$Variable, 
                          levels = c("Intercept", "Ideology", "Ideological Extremism", 
                                     "Partisanship", "Female", "College Education", "Age", 
                                     "Ideology × Ideological Extremism"))
glmAll_plot <- ggplot(
  glmAll[glmAll$Variable %in% c("Ideology", "Ideological Extremism", 
                                "Ideology × Ideological Extremism"),], 
  aes(x = as.factor(Year), y = Estimate, 
      ymin = Estimate - 1.96*SE, ymax = Estimate + 1.96*SE)) + 
  geom_hline(yintercept = 0, linetype = "dashed",colour = "gray") + 
  geom_pointrange() + facet_wrap(~Variable, nrow = 9, scales = "free") + 
  labs(x = "Year", y = "Logistic Regression Coefficients", 
       title = "Election Turnout (JESIII / IV / V)") + 
  theme(plot.title = element_text(hjust = 0.5))
plot(glmAll_plot)
